<template>
  <div v-if="visible" class="modal-overlay" @click.self="handleClose">
    <div class="modal-content">
      <h2>添加到歌单</h2>
      
      <div class="playlists-container">
        <div 
          class="playlist-item queue-item"
          :class="{ 'selected': addToPlayQueue }"
          @click="addToPlayQueue = !addToPlayQueue"
        >
          <div class="queue-icon">
            <i class="iconfont icon-playlist"></i>
          </div>
          <span class="playlist-name">添加到播放列表</span>
          <div class="checkbox">
            <div class="checkbox-inner"></div>
          </div>
        </div>
        
        <template v-if="loading">
          <div class="loading-skeleton">
            <div class="skeleton-item" v-for="i in 3" :key="i"></div>
          </div>
        </template>
        
        <template v-else-if="playlists.length === 0">
          <div class="empty-state">暂无歌单</div>
        </template>
        
        <template v-else>
          <div 
            v-for="playlist in playlists" 
            :key="playlist.id"
            class="playlist-item"
            :class="{ 'selected': selectedPlaylistIds.includes(playlist.id) }"
            @click="togglePlaylist(playlist.id)"
          >
            <img :src="playlist.cover" alt="封面" class="playlist-cover">
            <span class="playlist-name">{{ playlist.name }}</span>
            <div class="checkbox">
              <div class="checkbox-inner"></div>
            </div>
          </div>
        </template>
      </div>

      <!-- 按钮区域 -->
      <div class="modal-actions">
        <button class="cancel-btn" @click="handleClose">取消</button>
        <button 
          class="confirm-btn" 
          :disabled="selectedPlaylistIds.length === 0 && !addToPlayQueue || submitting"
          @click="handleConfirm"
        >
          {{ submitting ? '添加中...' : '确定' }}
        </button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { getUserPlaylists, addSongsToPlaylists } from '@/api/playList'
import type { PlayList } from '@/api/playList'
import { usePlayerStore } from '@/stores/player'
import { getMusicById } from '@/api/music'

const props = defineProps<{
  visible: boolean
  songIds: string[]
}>()

const emit = defineEmits<{
  (e: 'update:visible', value: boolean): void
  (e: 'success'): void
}>()

const loading = ref(false)
const submitting = ref(false)
const playlists = ref<PlayList[]>([])
const selectedPlaylistIds = ref<string[]>([])
const playerStore = usePlayerStore()
const addToPlayQueue = ref(false)

// 监听visible的变化
watch(() => props.visible, (newVal) => {
  if (newVal) {
    fetchUserPlaylists()
  }
})

// 获取用户歌单列表
const fetchUserPlaylists = async () => {
  loading.value = true
  try {
    const res = await getUserPlaylists()
    if (res.success && res.playlists) {
      playlists.value = res.playlists
    } else {
      ElMessage.error(res.message || '获取歌单失败')
    }
  } catch (error) {
    ElMessage.error('获取歌单失败')
  } finally {
    loading.value = false
  }
}

// 切换歌单选中状态
const togglePlaylist = (playlistId: string) => {
  const index = selectedPlaylistIds.value.indexOf(playlistId)
  if (index === -1) {
    selectedPlaylistIds.value.push(playlistId)
  } else {
    selectedPlaylistIds.value.splice(index, 1)
  }
}

// 确认添加
const handleConfirm = async () => {
  submitting.value = true
  try {
    // 如果选择了添加到播放列表
    if (addToPlayQueue.value) {
      for (const songId of props.songIds) {
        const song = await getMusicById(songId)
        await playerStore.appendMusicToQueue(song)
      }
    }

    // 如果选择了歌单
    if (selectedPlaylistIds.value.length > 0) {
      const res = await addSongsToPlaylists(props.songIds, selectedPlaylistIds.value)
      if (!res.success) {
        throw new Error(res.message || '添加到歌单失败')
      }
    }

    ElMessage.success('添加成功')
    emit('success')
    handleClose()
  } catch (error) {
    ElMessage.error('添加失败')
  } finally {
    submitting.value = false
  }
}

// 关闭模态框
const handleClose = () => {
  emit('update:visible', false)
  selectedPlaylistIds.value = []
}
</script>

<style scoped lang="scss">
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(10px);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-content {
  background: var(--bgc-color-1);
  border-radius: 24px;
  padding: 32px;
  width: 90%;
  max-width: 480px;
  border: 1px solid rgba(var(--theme-color), 0.2);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(12px);
}

.modal-content::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(145deg, rgba(var(--theme-color), 0.1), transparent);
  pointer-events: none;
}

h2 {
  color: var(--primary-color);
  margin-bottom: 24px;
  font-size: 1.5em;
  text-align: center;
  font-weight: 600;
  letter-spacing: 1px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  position: relative;
}

h2::after {
  content: '';
  position: absolute;
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 2px;
  background: linear-gradient(to right, transparent, var(--theme-color), transparent);
  border-radius: 2px;
}

.playlists-container {
  max-height: 400px;
  overflow-y: auto;
  margin: 0 -16px;
  padding: 0 16px;
}

.playlist-item, .queue-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  background: var(--bgc-color-2);
  border: 1px solid rgba(var(--theme-color), 0.1);
  border-radius: 12px;
  margin-bottom: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-2px);
    background: color-mix(in srgb, var(--bgc-color-2) 90%, var(--theme-color));
    border-color: rgba(var(--theme-color), 0.3);
  }
  
  &.selected {
    background: color-mix(in srgb, var(--theme-color) 20%, var(--bgc-color-2));
    border-color: var(--theme-color);
  }
}

.playlist-cover, .queue-icon {
  width: 48px;
  height: 48px;
  margin-right: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.playlist-cover {
  border-radius: 8px;
  object-fit: cover;
}

.queue-icon {
  color: var(--secondary-color);
  font-size: 24px;
  
  .iconfont {
    font-size: 28px;
    transition: all 0.3s ease;
  }
}

.checkbox {
  width: 24px;
  height: 24px;
  border: 2px solid var(--secondary-color);
  border-radius: 6px;
  margin-left: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  background: rgba(var(--bgc-color-1), 0.5);
}

.selected .checkbox {
  background: var(--theme-color);
  border-color: var(--theme-color);
}

.checkbox-inner {
  width: 12px;
  height: 12px;
  background: white;
  border-radius: 3px;
  opacity: 0;
  transform: scale(0.5);
  transition: all 0.3s ease;
}

.selected .checkbox-inner {
  opacity: 1;
  transform: scale(1);
}

.playlist-name {
  flex: 1;
  color: var(--primary-color);
  font-size: 1em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.queue-item:hover .queue-icon,
.queue-item.selected .queue-icon {
  color: var(--theme-color);
}

.queue-item {
  margin-bottom: 24px;
  position: relative;
  
  &::after {
    content: '';
    position: absolute;
    bottom: -12px;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(to right, 
      transparent,
      rgba(var(--theme-color), 0.2),
      transparent
    );
  }
}

.loading-skeleton {
  padding: 16px 0;
}

.skeleton-item {
  height: 72px;
  background: rgba(var(--theme-color), 0.1);
  border-radius: 12px;
  margin-bottom: 12px;
  animation: pulse 1.5s infinite;
}

.empty-state {
  text-align: center;
  padding: 32px 0;
  color: rgba(var(--primary-color), 0.6);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 16px;
  margin-top: 32px;
}

button {
  padding: 10px 24px;
  border-radius: 12px;
  font-size: 1em;
  cursor: pointer;
  transition: all 0.3s ease;
  letter-spacing: 0.5px;
  font-weight: 500;
}

.cancel-btn {
  background: transparent;
  border: 1px solid var(--secondary-color);
  color: var(--secondary-color);
  
  &:hover {
    background: rgba(var(--theme-color), 0.1);
    color: var(--primary-color);
    border-color: var(--theme-color);
  }
}

.confirm-btn {
  background: var(--theme-color);
  color: white;
  border: none;
  
  &:hover:not(:disabled) {
    background: var(--secondary-color);
  }
  
  &:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }
}

@keyframes pulse {
  0% { opacity: 0.6; }
  50% { opacity: 0.3; }
  100% { opacity: 0.6; }
}

/* 自定义滚动条样式 */
.playlists-container::-webkit-scrollbar {
  width: 8px;
}

.playlists-container::-webkit-scrollbar-track {
  background: rgba(var(--bgc-color-2), 0.4);
  border-radius: 4px;
}

.playlists-container::-webkit-scrollbar-thumb {
  background: rgba(var(--theme-color), 0.4);
  border-radius: 4px;
  border: 2px solid rgba(var(--bgc-color-2), 0.4);
}

.playlists-container::-webkit-scrollbar-thumb:hover {
  background: rgba(var(--theme-color), 0.6);
}
</style> 